package com.bhzyxy.love;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;

import com.bhzyxy.love.http.CommonCallback;
import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;

import java.io.File;
import java.util.Objects;

public class UploadPhotoActivity extends AppCompatActivity {

    private ImageView mainImageView;
    private File file;
    private AppCompatButton send = null;
    private AppCompatEditText et_text = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_love);
        mainImageView = findViewById(R.id.mainImageView);
        send = findViewById(R.id.send);
        et_text = findViewById(R.id.et_text);

        mainImageView.setOnClickListener(v -> {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            try {
                startActivityForResult(i, 0);
            } catch (ActivityNotFoundException exception) {
                Toast.makeText(UploadPhotoActivity.this, "no activit", Toast.LENGTH_SHORT).show();
            }
        });
       /* send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upLoad();
            }
        });*/
        send.setOnClickListener(v -> {
            //第一步：构建数据
            HttpParams httpParams = new HttpParams();
            httpParams.put("file", file);
            httpParams.put("text", String.valueOf(et_text.getText()));
            LoveActivity.commonHttp.post("member/addLove/", httpParams, new CommonCallback() {
                @Override
                public void onSuccess(String data) {
                    Toast.makeText(UploadPhotoActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(String msg) {
                    Toast.makeText(UploadPhotoActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            });
            finish();
        });

    }

    /*private void upLoad() {
        Bitmap bitmap;
        Bitmap bmpCompressed;
        for (int i = 0; i < list.size() - 1; i++) {
            bitmap = BitmapFactory.decodeFile(list.get(i));
            bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmpCompressed.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            System.out.println(data);
        }
    }*/


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            System.out.println("data null");
            return;
        }
        if (requestCode == 0) {
            Uri uri = data.getData();
            file = new File(Objects.requireNonNull(ImageTool.getImageAbsolutePath(this, uri)));
            Glide.with(this)
                    .load(ImageTool.getImageAbsolutePath(this, uri))
                    .dontAnimate()
                    .into(mainImageView);
        }
    }

}
